package com.xcl.util;

import com.zzg.entity.TmsRegion;
import com.zzg.util.UuidUtil;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName RegionUtil
 * @Description 地区工具类
 * @Author: xiaochaolei
 * @date 2021/7/30 15:14
 * @Version 1. 0
 */
public class RegionUtil {
    public static List<Map<String,String>> addressResolution(String address){
        String regex="(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        Matcher m= Pattern.compile(regex).matcher(address);
        String province=null,city=null,county=null,town=null,village=null;
        List<Map<String,String>> table=new ArrayList<Map<String,String>>();
        Map<String,String> row=null;
        while(m.find()){
            row=new LinkedHashMap<String,String>();
            province=m.group("province");
            row.put("province", province==null?"":province.trim());
            city=m.group("city");
            row.put("city", city==null?"":city.trim());
            county=m.group("county");
            row.put("county", county==null?"":county.trim());
            town=m.group("town");
            row.put("town", town==null?"":town.trim());
            village=m.group("village");
            row.put("village", village==null?"":village.trim());
            table.add(row);
        }
        return table;
    }

    public TmsRegion getTmsRegion(String dizhi){
        TmsRegion tmsRegion=new TmsRegion();

        tmsRegion.setRegionId(UuidUtil.getUuid());

        List<Map<String, String>> a = addressResolution(dizhi);
        for (int i = 0; i < a.size(); i++) {
            for(String s:a.get(i).keySet()){
                System.out.println("key : "+s+" value : "+a.get(i).get(s));
                if (s.equals("province")){
                    //省
                    tmsRegion.setProvince(a.get(i).get(s));
                }else if (s.equals("city")){
                    //市
                    tmsRegion.setCity(a.get(i).get(s));
                }else if (s.equals("county")){
                    //区
                    tmsRegion.setDistrict(a.get(i).get(s));
                }else {
                    tmsRegion.setDetailedAddress(dizhi);
                }
            }
        }
        System.out.println( tmsRegion.toString());
        return tmsRegion;
    }

    public static void main(String[] args) {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式


        String hehe = dateFormat.format( now );
        System.out.println(hehe);
    }
}
